package com.example.dou2204;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import com.blankj.utilcode.util.ToastUtils;
import com.example.dou2204.adapter.FragAdapter;
import com.example.dou2204.baseMVVM.BaseViewActivity;
import com.example.dou2204.databinding.ActivityDouBinding;
import com.example.dou2204.frag.FriendFragment;
import com.example.dou2204.frag.HomeFragment;
import com.example.dou2204.frag.MessageFragment;
import com.example.dou2204.frag.MineFragment;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class DouActivity extends BaseViewActivity<ActivityDouBinding> {
    List<Fragment> list = new ArrayList<>();
    FragAdapter fragAdapter;

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

        requestPermissions(new String[]{
                "android.permission.VIBRATE",
                "android.permission.RECORD_AUDIO" ,
                "android.permission.CAMERA",
                "android.permission.ACCESS_NETWORK_STATE",
                "android.permission.WRITE_EXTERNAL_STORAGE" ,
                "android.permission.ACCESS_FINE_LOCATION",
                "android.permission.GET_TASKS" ,
                "android.permission.ACCESS_WIFI_STATE" ,
                "android.permission.CHANGE_WIFI_STATE" ,
                "android.permission.WAKE_LOCK",
                "android.permission.MODIFY_AUDIO_SETTINGS",
                "android.permission.READ_PHONE_STATE",
                "android.permission.RECEIVE_BOOT_COMPLETED",
                "android.permission.FOREGROUND_SERVICE",
                "android.permission.ACCESS_COARSE_LOCATION"
        },100);




        list.add(new HomeFragment());
        list.add(new FriendFragment());
        list.add(new MessageFragment());
        list.add(new MineFragment());
        fragAdapter = new FragAdapter(this,list);
        binding.vp.setAdapter(fragAdapter);
        //上下联动
        binding.vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position){
                    case 0:
                        binding.bottom.getMenu().getItem(0).setChecked(true);
                        break;
                    case 1:
                        binding.bottom.getMenu().getItem(1).setChecked(true);
                        break;
                    case 2:
                        binding.bottom.getMenu().getItem(3).setChecked(true);
                        break;
                    case 3:
                        binding.bottom.getMenu().getItem(4).setChecked(true);
                        break;
                }
            }
        });
        //显示未读消息
        BadgeDrawable badge = binding.bottom.getOrCreateBadge(R.id.friend);
        BadgeDrawable badge1 = binding.bottom.getOrCreateBadge(R.id.message);
        badge1.setNumber(10);
        binding.bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home:
                        binding.vp.setCurrentItem(0);
                        break;
                    case R.id.friend:
                        binding.vp.setCurrentItem(1);
                        badge.setVisible(false);//点击未读消息消失
                        break;
                    case R.id.fabu:
                        //ToastUtils.showLong("点击了发布菜单，后续去实现功能");
                        startActivity(new Intent(DouActivity.this,ZhiboActivity.class));
                        break;
                    case R.id.message:
                        binding.vp.setCurrentItem(2);
                        break;
                    case R.id.mine:
                        binding.vp.setCurrentItem(3);
                        break;
                }
                return false;
            }
        });
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_dou;
    }

    /**
     *目的：给每一个fragment添加onActivityResult
     *
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            fragment.onActivityResult(requestCode,resultCode,data);
        }

    }
}